package Java8实战.Lambda实战._3_Lambda方法引用_Lambda;

import Java8实战.Lambda实战.Apple;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

public class _3_Lambda方法引用_Lambda {

    public static void main(String[] args) {

        List<Apple> appleList = Arrays.asList(
                new Apple("red",150),
                new Apple("green",123),
                new Apple("green",450)
        );

        //写法一
        appleList.sort((Apple apple1,Apple apple2) -> apple1.getWeight().compareTo(apple2.getWeight()));
        //写法二
        appleList.sort((a1,a2) -> a1.getWeight().compareTo(a2.getWeight()));
        //写法三
        Comparator<Apple> comparator = Comparator.comparing((Apple apple) -> apple.getWeight());
        appleList.sort(comparator);
        //写法四
        appleList.sort(Comparator.comparing((a) -> a.getWeight()));

        printByConsumer(appleList, (Apple apple) -> System.out.println("苹果的颜色为：" + apple.getColor() + "，苹果的重量为：" + apple.getWeight()));
    }

    public static int compareTo(int i1, int i2){
        if(i1 > i2){
            return 1;
        }
        else if(i1 == i2){
            return 0;
        }
        else{
            return -1;
        }
    }

    public static <T> void printByConsumer(List<T> list, Consumer<T> consumer){

        for (T t : list){
            consumer.accept(t);
        }
    }
}
